package vikaa.timelinepackage;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import vikaa.timelinepackage.ImageDownloader.ImageCallback;
import vikaa.tools.ImageBuilder;
import vikaa.tools.ValueClass;
import vikaa.wowo.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.widget.ImageView;

public class AsyLoadImg {
    /**
     * image loader
     */
    static ImageDownloader loader = null;
    /**
     * image builder
     */
    static ImageBuilder builder = null;
    
    private static Map<ImageView, String> imageViews; 
    
    static {  
        imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
    }  
    
    
    /**
     * 加载时间轴的Twitter的图片
     * @param url
     * @param imageView
     */
    public static void loadPic(String url,final ImageView imageView){
    	imageViews.put(imageView, url);  
        if(loader == null){
            loader = new ImageDownloader();
        }
        imageView.setImageResource(R.drawable.bg_timeline_content_pic_default);
        loader.loadDrawable(url, new ImageCallback() {
			public void imageLoaded(String url, Drawable imageDrawable) {
				String tag = imageViews.get(imageView);  
				if(tag != null && tag.equals(url)){
					if(imageDrawable!=null){
	                    imageView.setImageDrawable(imageDrawable);
	                }
				}
			}
        });
    }
    /**
     * 加载时间轴的封面图
     * @param url
     * @param imageView
     */
    public static void loadTimeBg(String url,final ImageView imageView){
    	imageViews.put(imageView, url);  
        if(loader == null){
            loader = new ImageDownloader();
        }
        imageView.setImageResource(R.drawable.bg_timeline_toppic_display);
        loader.loadDrawable(url, new ImageCallback() {
			public void imageLoaded(String url, Drawable imageDrawable) {
				String tag = imageViews.get(imageView);  
				if(tag != null && tag.equals(url)){
					if(imageDrawable!=null){
	                    imageView.setImageDrawable(imageDrawable);
	                }
				}
			}
        });
    }
    /**
     * 加载用户的头像
     * @param url
     * @param imageView
     */
    public static void loadAvater(String url,final ImageView imageView){
    	imageViews.put(imageView, url);  
        if(loader == null){
            loader = new ImageDownloader();
        }
        loader.loadDrawable(url, new ImageCallback() {
			public void imageLoaded(String url, Drawable imageDrawable) {
				String tag = imageViews.get(imageView);  
				if(tag != null && tag.equals(url)){
					if(imageDrawable!=null){
	                    imageView.setBackgroundDrawable(imageDrawable);
	                }
				}
			}
        });
    }
    
    public static void loadCornerAvater(String url,final ImageView imageView,final int pixel){
    	imageViews.put(imageView, url);  
        if(loader == null){
            loader = new ImageDownloader();
        }
        imageView.setImageResource(R.drawable.widget_dface);
        loader.loadDrawable(url, new ImageCallback() {
			public void imageLoaded(String url, Drawable imageDrawable) {
				String tag = imageViews.get(imageView);  
				if(tag != null && tag.equals(url)){
					if(imageDrawable!=null){
	                    imageView.setImageBitmap(toRoundCorner(imageDrawable, ValueClass.CORNER_PIXEL));
	                }
				}
			}
        });
    }
    public static void loadCircleAvater(String url,final ImageView imageView){
    	imageViews.put(imageView, url);  
        if(loader == null){
            loader = new ImageDownloader();
        }
        loader.loadDrawable(url, new ImageCallback() {
			public void imageLoaded(String url, Drawable imageDrawable) {
				String tag = imageViews.get(imageView);  
				if(tag != null && tag.equals(url)){
					if(imageDrawable!=null){
	                    imageView.setImageBitmap(toCircle(imageDrawable));
	                }
				}
			}
        });
    }
    /**
     * 加载本地的图片
     * @param url
     * @param imageView
     */
    public static void loadLocalPic(String url,final ImageView imageView){
    	imageViews.put(imageView, url);  
        if(builder == null){
            builder = new ImageBuilder();
        }
        imageView.setImageResource(R.drawable.select_image_default);
        builder.loadDrawable(url, new ImageBuilder.ImageCallback() {
			public void imageLoaded(String url, Drawable imageDrawable) {
				String tag = imageViews.get(imageView);  
				if(tag != null && tag.equals(url)){
					if(imageDrawable!=null){
	                    imageView.setImageDrawable(imageDrawable);
	                }
				}
			}
		});
    }
    public static void loadGridviewPhoto(String url,final ImageView imageView,final boolean flag,final Context context,final String role,final boolean already){
    	imageViews.put(imageView, url);  
    	if(loader == null){
            loader = new ImageDownloader();
        }
    	imageView.setImageResource(R.drawable.select_image_default);
        loader.loadDrawable(url, new ImageCallback() {
			public void imageLoaded(String url, Drawable imageDrawable) {
				String tag = imageViews.get(imageView);
				if(tag != null && tag.equals(url)) {
					if(imageDrawable!=null){
	                    imageView.setImageDrawable(imageDrawable);
	                }
                }
			}
        });
    }
    public static LayerDrawable makeMutlBmp(Bitmap bmp, boolean isChosen,Context mContext,String role,boolean already){
		LayerDrawable la;
    	Bitmap seletedBmp = null;
    	if(isChosen == true){
    		
    		if(Integer.valueOf(role) == ValueClass.ROLE_ADMIN || Integer.valueOf(role) == ValueClass.ROLE_CREATOR || already){
    			Bitmap stateBmp = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.photo_selected);
            	Drawable[] array = new Drawable[2];
            	array[0] = new BitmapDrawable(bmp);
            	array[1] = new BitmapDrawable(stateBmp);
            	la = new LayerDrawable(array);
            	la.setLayerInset(0, 0, 0, 0, 0);
            	la.setLayerInset(1, 0, 0, 0, 0 );
    		}
    		else{
    			Bitmap stateBmp = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.photo_state);
            	Drawable[] array = new Drawable[2];
            	array[0] = new BitmapDrawable(bmp);
            	array[1] = new BitmapDrawable(stateBmp);
            	la = new LayerDrawable(array);
            	la.setLayerInset(0, 0, 0, 0, 0);
            	la.setLayerInset(1, 0, 0, 0, 0 );
    		}
    	}
    	else{
    		seletedBmp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.photo_unselected);
    		Drawable[] array = new Drawable[2];
        	array[0] = new BitmapDrawable(bmp);
        	array[1] = new BitmapDrawable(seletedBmp);
        	la = new LayerDrawable(array);
        	la.setLayerInset(0, 0, 0, 0, 0);
        	la.setLayerInset(1, 0, 0, 0, 0 );
    		}
    	return la;	
    }
    
    public static Bitmap toRoundCorner(Drawable bitmap, int pixels) { 
	     Bitmap output = Bitmap.createBitmap(((BitmapDrawable)bitmap).getBitmap().getWidth(), 
	    		 ((BitmapDrawable)bitmap).getBitmap().getHeight(), Config.ARGB_8888); 
	     Canvas canvas = new Canvas(output); 
	     final int color = 0xff424242; 
	     final Paint paint = new Paint(); 
	     final Rect rect = new Rect(0, 0, ((BitmapDrawable)bitmap).getBitmap().getWidth(), ((BitmapDrawable)bitmap).getBitmap().getHeight()); 
	     final RectF rectF = new RectF(rect); 
	     final float roundPx = pixels; 
	     paint.setAntiAlias(true); 
	     canvas.drawARGB(0, 0, 0, 0); 
	     paint.setColor(color); 
	     canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
	     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
	     canvas.drawBitmap(((BitmapDrawable)bitmap).getBitmap(), rect, rect, paint); 
	     return output; 
    }
    public static Bitmap toCircle(Drawable bitmap) { 
	     Bitmap output = Bitmap.createBitmap(((BitmapDrawable)bitmap).getBitmap().getWidth(), 
	    		 ((BitmapDrawable)bitmap).getBitmap().getHeight(), Config.ARGB_8888); 
	     Canvas canvas = new Canvas(output); 
	     final int color = 0xff424242; 
	     final Paint paint = new Paint(); 
	     int w = ((BitmapDrawable)bitmap).getBitmap().getWidth();
	     int h = ((BitmapDrawable)bitmap).getBitmap().getHeight();
	     final Rect rect = new Rect(0, 0, w, h); 
	     paint.setAntiAlias(true); 
	     canvas.drawARGB(0, 0, 0, 0); 
	     paint.setColor(color); 
	     canvas.drawCircle(w/2, h/2, h/2, paint);
	     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
	     canvas.drawBitmap(((BitmapDrawable)bitmap).getBitmap(), rect, rect, paint); 
	     return output; 
   }
}